package java学习.JUC;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/6/1 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
class resource{
    private int nums = 0;
    private Lock lock =  new ReentrantLock();
    private Condition condition =  lock.newCondition();

    public void add() throws InterruptedException {
        lock.lock();
        try {
            if (nums!=0){
                condition.await();
            }
            nums++;
            System.out.println(Thread.currentThread().getName()+"+1 >> "+nums);

            condition.signalAll();
        }finally {
            lock.unlock();
        }

    }

    public void delete() throws InterruptedException {
        lock.lock();
        try {
//            if (nums!=1){
            if (nums==0){
                condition.await();
            }
            nums--;
            System.out.println(Thread.currentThread().getName()+"-1 >> "+nums);
            condition.signalAll();
        }finally {
            lock.unlock();
        }

    }

}

public class TestLock {

    public static void main(String[] args) {
        resource r = new resource();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.add();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"AA").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.delete();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"BB").start();

        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.add();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"CC").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.delete();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"DD").start();


        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.add();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"EE").start();
        new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < 10; i++) {
                    try {
                        r.delete();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        },"FF").start();



    }
}
